package com.freesky.biz.icbc.controller;

import com.freesky.biz.icbc.service.IIntegralExchangeService;
import com.freesky.biz.icbc.domain.TIntegralExchange;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 积分兑话费
 */
@RestController
@RequestMapping("/icbc/integral")
public class IntegralExchangeController extends BaseController {

    @Autowired
    private IIntegralExchangeService integralExchangeService;

    /**
     * 积分兑话费列表,processed=99代表卡单
     */
    @PreAuthorize("@ss.hasPermi('icbc:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(TIntegralExchange integralExchange) {
        startPage();
        //卡单（正在充值中，同时充值)
        if(StringUtils.isNotEmpty(integralExchange.getProcessed()) && "99".equalsIgnoreCase(integralExchange.getProcessed())){
            integralExchange.setProcessed("2");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR,-1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String processDate = sdf.format(calendar.getTime());
            integralExchange.setProcessDate(processDate);
        }
        List<TIntegralExchange> list = integralExchangeService.findIntegralMobileDetail(integralExchange);
        return getDataTable(list);
    }

    @Log(title = "积分兑换", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:integral:export')")
    @GetMapping("/export")
    public AjaxResult export(TIntegralExchange integralExchange,
                             HttpServletResponse response) {
        //卡单（正在充值中，同时充值)
        if(StringUtils.isNotEmpty(integralExchange.getProcessed()) && "99".equalsIgnoreCase(integralExchange.getProcessed())){
            integralExchange.setProcessed("2");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR,-1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String processDate = sdf.format(calendar.getTime());
            integralExchange.setProcessDate(processDate);
        }
        return integralExchangeService.exportFile(integralExchange);
    }

}
